package com.huangsiyuan.myepidemic.controller;

import com.huangsiyuan.myepidemic.beans.UserInfo;
import com.huangsiyuan.myepidemic.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
public class UserController {

    public  static Logger loggeer= Logger.getLogger(UserController.class);

    @Autowired
    private UserService uesrService;

    @RequestMapping("/login")
    public String login(UserInfo userInfo, Model model, HttpSession session){
        loggeer.debug("login方法被執行了！！！"+userInfo.getAccount()+"   "+userInfo.getPassword());
        //通過業務邏輯層的bean獲取該賬號對應的用戶信息
        UserInfo user=this.uesrService.findByAccount(userInfo.getAccount());
        if(user==null){
            //賬號不正確
            model.addAttribute("msg","賬號不正確！！！");
            return "login";
        }
        if(user.getPassword().equals(userInfo.getPassword())){
            //登錄成功
            //将当前用户的信息保存到session中
            session.setAttribute("loginedUser",user);
            return "redirect:/main.jsp";
        }else{
            //密碼不正確
            model.addAttribute("msg","密碼不正確！！");
            return "login";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //清理Session
        session.invalidate();
        return "redirect:/epidemic.jsp";
    }
}
